package com.boy.controller.admin;

import com.boy.tool.utils.JwtUtils;
import com.boy.entity.Account;
import com.boy.service.AccountService;
import com.boy.tool.annotate.Arbitrarily;
import com.boy.tool.domain.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author whd
 * @date 2024/06/14 08:50:58
 */
@RestController
@RequestMapping("/auth")
public class AdminAuthController {
    @Autowired
    private AccountService accountService;

    @Arbitrarily
    @PostMapping("/login")
    public ResultVo login(@RequestParam(value = "phone") String phone, @RequestParam(value = "password") String password, HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        Account account = accountService.login(phone, password, ip);
        String token = JwtUtils.getJwtToken(account.getId() + "", account.getPhone());
        return ResultVo.success("登录成功", token);
    }
    @PostMapping("/me")
    public ResultVo me(@RequestHeader("Authorization") String token) {
        String id = JwtUtils.getMemberIdByJwtToken(token);
        Account account = accountService.getById(id);
        return ResultVo.success(account);
    }
}
